package com.piggy.logi.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.piggy.common.core.constant.SecurityConstants;
import com.piggy.common.core.domain.R;
import com.piggy.common.core.utils.FeignResultUtils;
import com.piggy.common.core.utils.PageUtils;
import com.piggy.common.core.web.page.PagePlus;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.logi.sys.vo.LogiCustomerExtVo;
import com.piggy.sys.api.RemoteDistrictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.piggy.logi.sys.bo.LogiCustomerBookQueryBo;
import com.piggy.logi.sys.bo.LogiCustomerBookEditBo;
import com.piggy.logi.sys.domain.LogiCustomerBook;
import com.piggy.logi.sys.mapper.LogiCustomerBookMapper;
import com.piggy.logi.sys.vo.LogiCustomerBookVo;
import com.piggy.logi.sys.service.ILogiCustomerBookService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 客户收发货地址管理Service业务层处理
 *
 * @author piggy
 * @date 2023-08-16
 */
@Slf4j
@Service
public class LogiCustomerBookServiceImpl extends ServiceImpl<LogiCustomerBookMapper, LogiCustomerBook> implements ILogiCustomerBookService {

    @Resource
    private RemoteDistrictService districtService;

    @Override
    public LogiCustomerBookVo queryById(Long id) {
        LogiCustomerBookVo vo = getVoById(id, LogiCustomerBookVo.class);
        if (ObjectUtil.isEmpty(vo)) {
            return null;
        }
        return fillData(Lists.newArrayList(vo)).get(0);
    }

    @Override
    public TableDataInfo<LogiCustomerBookVo> queryPageList(LogiCustomerBookQueryBo bo) {
        PagePlus<LogiCustomerBook, LogiCustomerBookVo> result = pageVo(PageUtils.buildPagePlus(bo, true), buildQueryWrapper(bo).orderByAsc(LogiCustomerBook::getSort), LogiCustomerBookVo.class);
        result.setRecordsVo(fillData(result.getRecordsVo()));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<LogiCustomerBookVo> queryList(LogiCustomerBookQueryBo bo) {
        return fillData(listVo(buildQueryWrapper(bo).orderByAsc(LogiCustomerBook::getSort), LogiCustomerBookVo.class));
    }

    private LambdaQueryWrapper<LogiCustomerBook> buildQueryWrapper(LogiCustomerBookQueryBo bo) {
        LambdaQueryWrapper<LogiCustomerBook> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, LogiCustomerBook::getId, bo.getId());
        lqw.eq(bo.getCustomerId() != null, LogiCustomerBook::getCustomerId, bo.getCustomerId());
        lqw.like(StrUtil.isNotBlank(bo.getContactName()), LogiCustomerBook::getContactName, bo.getContactName());
        lqw.eq(StrUtil.isNotBlank(bo.getPhone()), LogiCustomerBook::getPhone, bo.getPhone());
        lqw.eq(bo.getSort() != null, LogiCustomerBook::getSort, bo.getSort());
        lqw.eq(bo.getBookDistrict() != null, LogiCustomerBook::getBookDistrict, bo.getBookDistrict());
        lqw.eq(StrUtil.isNotBlank(bo.getBookAddress()), LogiCustomerBook::getBookAddress, bo.getBookAddress());
        lqw.eq(bo.getBookLat() != null, LogiCustomerBook::getBookLat, bo.getBookLat());
        lqw.eq(bo.getBookLng() != null, LogiCustomerBook::getBookLng, bo.getBookLng());
        lqw.in(CollUtil.isNotEmpty(bo.getIdList()), LogiCustomerBook::getId, bo.getIdList());
        lqw.in(CollUtil.isNotEmpty(bo.getCustomerIdList()), LogiCustomerBook::getCustomerId, bo.getCustomerIdList());
        return lqw;
    }

    public List<LogiCustomerBookVo> fillData(List<LogiCustomerBookVo> bookExtVos) {

        List<String> distIds = bookExtVos.stream().map(vo-> ObjectUtil.defaultIfNull(vo.getBookDistrict(), 0).toString()).distinct().collect(Collectors.toList());
        distIds.add("-1");

        R<Map<String, String>> distMapR = districtService.getDistrictMap(distIds, SecurityConstants.INNER);
        FeignResultUtils.throwIfFailed(distMapR);

        bookExtVos.stream().peek(Any-> {
            Any.setBookDistrictName(ObjectUtil.defaultIfNull(distMapR.getData(), new HashMap<String,String>()).get(ObjectUtil.defaultIfNull(Any.getBookDistrict(), 0).toString()));
        }).collect(Collectors.toList());

        return Lists.newArrayList(bookExtVos);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByAddBo(LogiCustomerBookEditBo bo) {
        LogiCustomerBook add = BeanUtil.toBean(bo, LogiCustomerBook.class);
        return save(add);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByEditBo(LogiCustomerBookEditBo bo) {
        LogiCustomerBook update = BeanUtil.toBean(bo, LogiCustomerBook.class);
        return updateById(update);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchByAddBo(List<LogiCustomerBookEditBo> boList) {
        if (CollUtil.isEmpty(boList)) {
            return false;
        }

        List<LogiCustomerBook> bookList = BeanUtil.copyToList(boList, LogiCustomerBook.class);
        return this.saveBatch(bookList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean batchByUpdateBo(List<LogiCustomerBookEditBo> boList) {
        if (CollUtil.isEmpty(boList)) {
            return false;
        }

        List<LogiCustomerBook> bookList = BeanUtil.copyToList(boList, LogiCustomerBook.class);
        return this.saveOrUpdateBatch(bookList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
